﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Obs;
using Aandelendelen.SiteMessage;

namespace Aandelendelen.Controls
{
	public partial class SiteMessageControl : AuthenticationRequiredBaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.DisplaySiteMessages();
		}

		private void DisplaySiteMessages()
		{
			string userId = this.UserInfo.Gebruikersnaam.ToLower();

			string doAll = this.Request["all"];

			if (doAll == null)
				doAll = "0";

			DateTime previousLogin = this.UserInfo.PreviousLogin;

			SiteMessageCollection messages;
			if (doAll != "0")
				messages = new SiteMessageCollection(userId);
			else
				messages = new SiteMessageCollection(userId, previousLogin);

			if (messages.Count == 0 && doAll == "0")
			{
				Response.Redirect("/MyCredits.aspx", true);
			}

			if (messages.Count == 0)
			{
				this.lblNoMessages.Visible = true;
			}
			else
			{
				foreach (SiteMessage.SiteMessage msg in messages)
				{
					TableRow row = new TableRow();

					TableCell cellDate = new TableCell();
					cellDate.Width = Unit.Pixel(192);
					cellDate.HorizontalAlign = HorizontalAlign.Left;
					Label l = new Label();
					l.Text = msg.TimeStamp.ToString("ddd, dd-MMM-yyyy HH:mm");
					l.CssClass = "newsdate";
					cellDate.Controls.Add(l);

					row.Cells.Add(cellDate);
					this.Table1.Rows.Add(row);

					TableRow rowBody = new TableRow();

					TableCell cellBody = new TableCell();
					cellBody.HorizontalAlign = HorizontalAlign.Left;
					Label lBody = new Label();
					lBody.Text = msg.Body + "<hr />";
					cellBody.Controls.Add(lBody);

					rowBody.Cells.Add(cellBody);
					this.Table1.Rows.Add(rowBody);
				}
			}

		}
	}
}